class EventEmit {
  constructor() {
    this.events = {};
  }

  on(name, cb) {
    // 添加进同个组
    this.events[name] = this.events[name] || [];
    this.events[name].push(cb);
  }

  off(name, cb) {
    this.events[name] = this.events[name].filter((fn) => fn !== cb);
  }

  once(name, cb) {
    // 只执行一次
    const _cb = (...rest) => {
      this.off(name, _cb);
      cb(...rest);
    };

    this.on(name, _cb);
  }

  emit(name, ...rest) {
    this.events[name]?.forEach((cb) => {
      cb(...rest);
    });
  }
}

// egg
const eventBUs = new EventEmit();

eventBUs.on("a", (data) => {
  console.log("a==>", data);
});

eventBUs.once("b", (data) => {
  console.log("b==>", data);
});

eventBUs.emit("a", "我是a");
eventBUs.emit("a", "我是a");
eventBUs.emit("b", "我是bb");
eventBUs.emit("b", "我是bb");
